//
//  YMHomeChooseUserView.m
//  YMMedical
//
//  Created by y s on 2023/7/18.
//

#import "YMHomeChooseUserView.h"


#define max_item_count 6
@interface YMHomeChooseUserView ()

@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UIImageView *imgvBg;
@property (nonatomic, strong) UIView *vContent;

@property (nonatomic, strong) UIScrollView *svMain;

@end


@implementation YMHomeChooseUserView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self addSubview:self.vBg];
    [self.vBg addSubview:self.imgvBg];
    [self.imgvBg addSubview:self.svMain];
    [self.svMain addSubview:self.vContent];
    
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TOP_BAR_HEIGHT-7);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo([SFitUtil fit:88+30]);
    }];
    [self.imgvBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.svMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(7+15);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-15);
        make.height.mas_equalTo([SFitUtil fit:30]*3);
    }];
    [self.vContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
        make.width.equalTo(self.svMain);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
}

- (void)show{
    UIWindow *keyWindow = [SThemeUtil keyWindow];
    for (UIView *item in keyWindow.subviews) {
        if ([item isKindOfClass:[self class]]){
            return;
        }
    }
    [keyWindow addSubview:self];
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

- (void)dismiss{
    [self removeFromSuperview];
}

- (void)setArrList:(NSArray *)arrList{
    _arrList = arrList;
    if (arrList.count > max_item_count){
        _svMain.showsVerticalScrollIndicator = YES;
        _svMain.scrollEnabled = YES;
    }else{
        _svMain.showsVerticalScrollIndicator = NO;
        _svMain.scrollEnabled = NO;
    }
    [self.svMain mas_updateConstraints:^(MASConstraintMaker *make) {
        if (arrList.count > max_item_count){
            make.height.mas_equalTo([SFitUtil fit:30]*max_item_count);
        }else{
            make.height.mas_equalTo([SFitUtil fit:30]*arrList.count);
        }
    }];
    
    [self.vContent removeAllSubviews];
    int index = 0;
    for (NSString *strName in self.arrList) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:strName forState:UIControlStateNormal];
        [btn setTitleColor:STheme.D1 forState:UIControlStateNormal];
        btn.titleLabel.font = [SFitUtil fitFont:12];
//        btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [btn addTarget:self action:@selector(chooseUserClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 200+index;
        [self.vContent addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo([SFitUtil fit:30]*index);
            make.height.mas_equalTo([SFitUtil fit:30]);
            if (index == self.arrList.count-1){
                make.bottom.mas_equalTo(0);
            }
        }];
        index++;
    }
}
- (void)setYPoint:(CGPoint)yPoint{
    _yPoint = yPoint;
    if (yPoint.y > 0){
        [self.vBg mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(yPoint.y);
            make.left.mas_equalTo(yPoint.x);
            if (self.yWidth > 0){
                make.width.mas_equalTo(self.yWidth);
            }else{
                make.width.mas_equalTo([SFitUtil fit:88+30]);
            }
        }];
    }
}

- (void)chooseUserClick:(UIButton *)sender{
    NSInteger index = sender.tag - 200;
    if (self.chooseUserBlock){
        self.chooseUserBlock(index);
    }
    [self dismiss];
}

#pragma mark -- setter and getter
- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
    }
    return _vBg;
}
- (UIImageView *)imgvBg{
    if (_imgvBg == nil) {
        _imgvBg = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamed:@"home_choose_user_pop_bg"];
        UIEdgeInsets edges = UIEdgeInsetsMake(20, 20, 20, 20);
        _imgvBg.image = [image resizableImageWithCapInsets:edges resizingMode:UIImageResizingModeStretch];
        _imgvBg.userInteractionEnabled = YES;
    }
    return _imgvBg;
}
- (UIScrollView *)svMain{
    if (_svMain == nil){
        _svMain = [[UIScrollView alloc] init];
        _svMain.showsVerticalScrollIndicator = YES;
        _svMain.showsHorizontalScrollIndicator = NO;
    }
    return _svMain;
}
- (UIView *)vContent{
    if (_vContent == nil){
        _vContent = [[UIView alloc] init];
    }
    return _vContent;
}

@end
